package com.weilai.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtil {
    //阿里域名
    public static final String ALI_DOMAIN = "https://haven-file.oss-cn-beijing.aliyuncs.com/";

    public static String upload(MultipartFile file) throws IOException {
        //生成文件名
        String fileName = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
        //地域节点
        String endpoint = "http://oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI5t5rygvvrkCZ8sV32Erp";
        String accessKeySecret = "7vAdRMJnCUgqOZnDLyfhfUtEEpkDqj";
        //OSS客户端对象
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject("haven-file",fileName,file.getInputStream());
        ossClient.shutdown();
        return ALI_DOMAIN + fileName;
    }
}
